package com;

import com.fz.entity.Student;
import com.fz.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

/**
 * Created by webrx on 2017-08-28.
 */
public class Demo {

    protected SqlSessionFactory sf;
    protected SqlSession session;

    @Before
    public void init(){
        try {
            this.sf = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml"));
            this.session = this.sf.openSession();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Test
    public void add(){
        StudentMapper sdao = this.session.getMapper(StudentMapper.class);
        sdao.insert(new Student("小刘1",60,"北京"));
        sdao.insert(new Student("小刘2",70,"上海"));
        sdao.insert(new Student("小刘3",80,"广州"));
        sdao.insert(new Student("小刘4",90,"深圳"));
    }

    @Test
    public void show(){
        //System.out.println("程序。。。。。。");
        StudentMapper sdao = this.session.getMapper(StudentMapper.class);
        Student st = sdao.queryById(18);
        System.out.println(st);
    }

    @After
    public void close(){
        if(this.session!=null){
            this.session.commit();
            this.session.clearCache();
            this.session.close();
        }
    }
}
